package com.firesrc.web;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;
import jodd.db.pool.CoreConnectionPool;

import com.firesrc.bean.Record;
import com.firesrc.db.DBManager;

/**
 * Servlet implementation class QueryMailByTopic
 */
public class QueryMailByTopic extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public QueryMailByTopic() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		response.setCharacterEncoding("GBK");
		response.setContentType("text/html; charset=gb2312");
		request.setCharacterEncoding("UTF-8");
		String topic = request.getParameter("topic");
		//		topic = new String(request.getParameter("topic").getBytes("ISO8859_1"));
		// tmpKeyword = new
		// String(request.getParameter("keyword").getBytes("UTF-8"), "GBK");
		System.out.println("你查询的关键词是:" + topic);
		CoreConnectionPool cp = DBManager.getPublicConnectionPool();
		new DbThreadSession(cp);
		DbOomQuery q = new DbOomQuery("select * from record where topic='" + topic
				+ "' and  (mailstate is null or mailstate='0')");
		List<Record> list = q.list(Record.class);

		Set<Record> set = new HashSet<Record>();
		for (Record record : list) {
			set.add(record);
		}
		request.setAttribute("set", set);
		request.setAttribute("topic", topic);
		request.getRequestDispatcher("detailMailListByTopic.jsp").forward(request, response);
	}

}
